#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @time     : 2020/12/11 15:55
# @Author   : BYF
# file      : ding_talk.py
# @version  :

import time
import hmac
import base64
import hashlib
import requests
import urllib.parse

class DingTalk():
    def __init__(self):
        pass

    @staticmethod
    def sign(self, secret):
        """加签"""
        timestamp = str(round(time.time() * 1000))
        secret_enc = secret.encode('utf-8')
        string_to_sign = f'{timestamp}\n{secret}'
        string_to_sign_enc = string_to_sign.encode('utf-8')
        hmac_code = hmac.new(secret_enc, string_to_sign_enc, digestmod=hashlib.sha256).digest()
        sign = urllib.parse.quote_plus(base64.b64encode(hmac_code))
        print(timestamp)
        print(sign)
        return timestamp, sign

    @staticmethod
    def push(self, access_token, timestamp, sign, data):
        """推送钉钉消息"""
        url = f"https://oapi.dingtalk.com/robot/send?access_token={access_token}&timestamp={timestamp}&sign={sign}"

        headers = {
            'Content-Type': 'application/json'
        }
        '''
                # 消息类型为 text类型
                data_text = {
                    "msgtype": "text",
                    "text": {
                        "content": "我就是我, 是不一样的烟火"
                    },
                }

                # 消息类型为 link类型
                data_link = {
                    "msgtype": "link",
                    "link": {
                        "text": "这个即将发布的新版本，创始人xx称它为红树林。而在此之前，每当面临重大升级，产品经理们都会取一个应景的代号，这一次，为什么是红树林",
                        "title": "时代的火车向前开",
                        "picUrl": "",
                        "messageUrl": "https://www.dingtalk.com/s?__biz=MzA4NjMwMTA2Ng==&mid=2650316842&idx=1&sn=60da3ea2b29f1dcc43a7c8e4a7c97a16&scene=2&srcid=09189AnRJEdIiWVaKltFzNTw&from=timeline&isappinstalled=0&key=&ascene=2&uin=&devicetype=android-23&version=26031933&nettype=WIFI"
                    }
                }

                # 消息类型为 markdown类型
                data_markdown = {
                    "msgtype": "markdown",
                    "markdown": {
                        "title": "杭州天气",
                        "text": "#### 杭州天气 @150XXXXXXXX \n> 9度，西北风1级，空气良89，相对温度73%\n> ![screenshot](https://img.alicdn.com/tfs/TB1NwmBEL9TBuNjy1zbXXXpepXa-2400-1218.png)\n> ###### 10点20分发布 [天气](https://www.dingtalk.com) \n"
                    },
                    "at": {
                        "atMobiles": [
                            "150XXXXXXXX"
                        ],
                        "isAtAll": false
                    }
                }

                # 消息类型为 整体跳转ActionCard类型
                data_action_card = {
                    "actionCard": {
                        "title": "乔布斯 20 年前想打造一间苹果咖啡厅，而它正是 Apple Store 的前身",
                        "text": """![screenshot](https://gw.alicdn.com/tfs/TB1ut3xxbsrBKNjSZFpXXcXhFXa-846-786.png)
                        ### 乔布斯 20 年前想打造的苹果咖啡厅
                        Apple Store 的设计正从原来满满的科技感走向生活化，而其生活化的走向其实可以追溯到20年前苹果一个建立咖啡馆的计划""",
                        "btnOrientation": "0",
                        "singleTitle": "阅读全文",
                        "singleURL": "https://www.dingtalk.com/"
                    },
                    "msgtype": "actionCard"
                }

                # 消息类型为 独立跳转ActionCard类型
                data_action_card1 = {
                    "actionCard": {
                        "title": "乔布斯 20 年前想打造一间苹果咖啡厅，而它正是 Apple Store 的前身",
                        "text": """![screenshot](https://gw.alicdn.com/tfs/TB1ut3xxbsrBKNjSZFpXXcXhFXa-846-786.png) 
                        ### 乔布斯 20 年前想打造的苹果咖啡厅 
                        Apple Store 的设计正从原来满满的科技感走向生活化，而其生活化的走向其实可以追溯到 20 年前苹果一个建立咖啡馆的计划""",
                        "btnOrientation": "0",
                        "btns": [
                            {
                                "title": "内容不错",
                                "actionURL": "https://www.dingtalk.com/"
                            },
                            {
                                "title": "不感兴趣",
                                "actionURL": "https://www.dingtalk.com/"
                            }
                        ]
                    },
                    "msgtype": "actionCard"
                }

                # 消息类型为 FeedCard类型
                data_feed_card = {
                    "feedCard": {
                        "links": [
                            {
                                "title": "时代的火车向前开",
                                "messageURL": "https://www.dingtalk.com/s?__biz=MzA4NjMwMTA2Ng==&mid=2650316842&idx=1&sn=60da3ea2b29f1dcc43a7c8e4a7c97a16&scene=2&srcid=09189AnRJEdIiWVaKltFzNTw&from=timeline&isappinstalled=0&key=&ascene=2&uin=&devicetype=android-23&version=26031933&nettype=WIFI",
                                "picURL": "https://gw.alicdn.com/tfs/TB1ayl9mpYqK1RjSZLeXXbXppXa-170-62.png"
                            },
                            {
                                "title": "时代的火车向前开2",
                                "messageURL": "https://www.dingtalk.com/s?__biz=MzA4NjMwMTA2Ng==&mid=2650316842&idx=1&sn=60da3ea2b29f1dcc43a7c8e4a7c97a16&scene=2&srcid=09189AnRJEdIiWVaKltFzNTw&from=timeline&isappinstalled=0&key=&ascene=2&uin=&devicetype=android-23&version=26031933&nettype=WIFI",
                                "picURL": "https://gw.alicdn.com/tfs/TB1ayl9mpYqK1RjSZLeXXbXppXa-170-62.png"
                            }
                        ]
                    },
                    "msgtype": "feedCard"
                }
                '''
        res = requests.post(url=url, json=data, headers=headers)
        return res